package com.tuananh.radnom.service.monitor.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.tuananh.radnom.model.netpoller.PollScheduleInfo;
import com.tuananh.radnom.model.netpoller.PollPackage;
import com.tuananh.radnom.model.netpoller.PollingConfiguration;
import com.tuananh.radnom.model.netservice.NetService;
import com.tuananh.radnom.persist.imp.xml.NetIntXmlDAO;
import com.tuananh.radnom.persist.imp.xml.NetPollerXmlDAO;
import com.tuananh.radnom.persist.imp.xml.NetServiceXmlDAO;
import com.tuananh.radnom.service.config.impl.PollService;

public class PollDaemon implements Runnable {
	public NetIntXmlDAO netIntDAO;
	public NetServiceXmlDAO netServiceDAO;
	public NetPollerXmlDAO netPollerDAO;

	public NetIntXmlDAO getNetIntDAO() {
		return netIntDAO;
	}

	public void setNetIntDAO(NetIntXmlDAO netIntDAO) {
		this.netIntDAO = netIntDAO;
	}

	public NetServiceXmlDAO getNetServiceDAO() {
		return netServiceDAO;
	}

	public void setNetServiceDAO(NetServiceXmlDAO netServiceDAO) {
		this.netServiceDAO = netServiceDAO;
	}

	public NetPollerXmlDAO getNetPollerDAO() {
		return netPollerDAO;
	}

	public void setNetPollerDAO(NetPollerXmlDAO netPollerDAO) {
		this.netPollerDAO = netPollerDAO;
	}

	public static PollService pollService;

	public PollService getPollService() {
		return pollService;
	}

	public void setPollService(PollService pollService) {
		this.pollService = pollService;
	}

	public static enum DaemonState {
		Stopped, Started, Paused
	}

	public PollDaemon() {

	}

	public PollDaemon(PollService pollService) {
		this.pollService = pollService;
	}

	public static ScheduledThreadPoolExecutor ste = new ScheduledThreadPoolExecutor(
			100);
	public static ArrayList<PollRunner> listPollRunner = new ArrayList<PollRunner>();

	public static boolean addPollRunner(PollRunner pollRunner) {
		ArrayList<PollRunner> list = getPollRunner(pollRunner.getPollScheduleInfo()
				.getPackageName(), pollRunner.getPollScheduleInfo().getNetServiceName());
		if (list.size() == 0) {
			listPollRunner.add(pollRunner);
			ste.scheduleAtFixedRate(pollRunner, 0, pollRunner.getPollScheduleInfo()
					.getInterval(), TimeUnit.SECONDS);
		}
		return false;
	}

	public static boolean removePollRunner(String packageName,
			String serviceName) {
		ArrayList<PollRunner> list = getPollRunner(packageName, serviceName);
		for (int i = 0; i < list.size(); i++) {
			PollRunner pollRunner = list.get(i);
			listPollRunner.remove(i);
			System.out.println(" " + ste.remove(pollRunner));
			break;
		}
		return true;
	}

	public static ArrayList<PollRunner> getPollRunner(String packageName,
			String serviceName) {
		ArrayList<PollRunner> list = new ArrayList<PollRunner>();

		for (int i = 0; i < listPollRunner.size(); i++) {
			PollRunner pollRunner = listPollRunner.get(i);
			if ((pollRunner.getPollScheduleInfo().getPackageName().equals(packageName) || packageName == null)
					&& (pollRunner.getPollScheduleInfo().getNetServiceName()
							.equals(serviceName) || serviceName == null))
				list.add(pollRunner);
		}
		return list;
	}

	public DaemonState state;
	public static Date startTime;

	public void run() {
		// TODO Auto-generated method stub
		state = DaemonState.Started;

		ArrayList<PollPackage> listPackage = pollService.getAllPackage();
		if (listPackage != null && listPackage.size() != 0)
			for (PollPackage pollPack : listPackage) {
				ArrayList<NetService> listService = pollPack.getServices();
				if (listService != null) {
					for (NetService netService : listService) {
						addPollRunner(new PollRunner(new PollScheduleInfo(
								pollPack.getName(), netService.getName(),
								netService.getInterval())));
					}
				}
			}
		while (!state.equals(DaemonState.Stopped)) {
			if (state.equals(DaemonState.Paused)) {
			}
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		ste.shutdown();
	}

	public boolean stop() {
		state = DaemonState.Stopped;
		return true;
	}

}
